package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.UserState;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.CodeConst;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.MD5Util;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import cn.net.shuxing.yifu.common.util.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/front")
public class MyInfoSafeCentreController extends BaseController {

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private CashWalletService cashWalletService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserService userService;

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private UserPasswordService userPasswordService;

    @Autowired
    private UserInfoBindService userInfoBindService;

    @Autowired
    private SmsUserService smsUserService;

    @Autowired
    private FrontCommonService frontCommonService;

    @Autowired
    private MailService mailService;

    @Autowired
    private UserRealInfoService userRealInfoService;


    //获取短信,生成redis    username:15088728888:verifyCode 888888
//    /**
//     * 更改用户密码
//     *
//     * @param userId
//     * @param phoneNum
//     * @param oldPassword
//     * @param code
//     * @param newPassword
//     * @param capitalPassword
//     * @return
//     */
//    @ApiOperation(value = "更改用户密码",httpMethod = "PUT")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userId",value = "用户id", required = true,dataType = "Integer", paramType = "query"),
//            @ApiImplicitParam(name = "phoneNum",value = "手机号",required = true, dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "oldPassword",value = "旧密码", required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "code",value = "验证码", required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "newPassword",value = "新密码", required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "capitalPassword",value = "资金密码", required = true,dataType = "String",paramType = "query")
//    })
//    @RequestMapping("/my/safeCentre/changeUserPassword")
//    public BaseDto changeUserPassword(int userId, String phoneNum, String oldPassword, String code, String newPassword, String capitalPassword) {
//        BaseDto baseDto = new BaseDto();
//        User user = userService.getByPrimaryKey(userId);
//        //Jedis //jedis = jedisUtil.getJedis();
//        String verifyCode = jedisUtil.get(RedisKeyCreator.capitalPasswordVerifyCodeKey(phoneNum));
//        jedisUtil.set(RedisKeyCreator.login(userId), JSON.toJSONString(user));
//        //jedisUtil.close();
//
//        if (code.equals(verifyCode) == false) {
//            baseDto.setCode(CodeConst.ERROR);
//            baseDto.setMsg("验证码错误!");
//        } else if (oldPassword.equals(user.getPassword()) == false) {
//            baseDto.setCode(CodeConst.ERROR);
//            baseDto.setMsg("新密码不能与旧密码重复!");
//        } else if (capitalPassword.equals(userPasswordService.getByPrimaryKey(userId).getCapitalPassword()) == false) {
//            baseDto.setCode(CodeConst.ERROR);
//            baseDto.setMsg("资金密码错误!");
//        } else {
//            User user1 = userService.searchByUserid(userId);
//            user1.setPassword(newPassword);
//            baseDto.setData(user1);
//        }
//
//        return baseDto;
//    }


    /**
     * 发送 修改资金密码 的验证码
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "发送 修改资金密码 的验证码", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/capitalPassword/verifyCode")
    public BaseDto sendCapitalPasswordVerifyCode(Integer userId) {
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        if (userInfoBind == null) {
            BaseDto baseDto = new BaseDto();
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("该用户不存在");
            return baseDto;
        }
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        if (userRealInfo == null)
            return BaseDto.error("请先完成实名认证");
        if (userRealInfo.getVerified() == 0)
            return BaseDto.error("请先完成实名认证");
        String phoneNum = userInfoBindService.getPhoneNumByUserId(userId);
        if (!StringUtils.hasText(phoneNum)) {
            BaseDto baseDto = new BaseDto();
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("该用户还未绑定手机号,请先绑定手机号");
            return baseDto;
        }
        smsUserService.sendSMS(phoneNum, RedisKeyCreator.capitalPasswordVerifyCodeKey(phoneNum));
        BaseDto baseDto = new BaseDto();
        baseDto.setData("success");
        return baseDto;
    }

    /**
     * 发送 修改登录密码 的验证码
     * @param userId
     * @return
     */
//    @ApiOperation(value = "发送 修改登录密码 的验证码",httpMethod = "GET")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userId",value = "用户id",required = true, dataType = "Integer",paramType = "query")
//    })
//    @RequestMapping("/my/safeCentre/loginPassword/verifyCode")
//    public BaseDto sendLoginPasswordVerifyCode(Integer userId)
//    {
//        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
//        if (userInfoBind == null)
//        {
//            BaseDto baseDto = new BaseDto();
//            baseDto.setCode(CodeConst.ERROR);
//            baseDto.setMsg("该用户不存在");
//            return baseDto;
//        }
//        String phoneNum = userInfoBind.getPhoneNum();
//        if (!StringUtils.hasText(phoneNum))
//        {
//            BaseDto baseDto = new BaseDto();
//            baseDto.setCode(CodeConst.ERROR);
//            baseDto.setMsg("该用户还未绑定手机号,请先绑定手机号");
//            return baseDto;
//        }
//        smsUserService.sendSMS(phoneNum,RedisKeyCreator.loginPasswordVerifyCodeKey(phoneNum));
//        BaseDto baseDto = new BaseDto();
//        baseDto.setData("success");
//        return baseDto;
//    }

    /**
     * 更改资金密码
     *
     * @param userId          用户id
     * @param code            验证码
     * @param capitalPassword 资金密码
     * @return
     */
    @ApiOperation(value = "更改资金密码", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "newCapitalPassword", value = "新资金密码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/ChangeCapitalPassword")
    public BaseDto changeCapitalPassword(Integer userId, String code, String capitalPassword) {
        BaseDto baseDto1 = checkUserId(userId);
        if (baseDto1 != null && baseDto1.getCode() == CodeConst.ERROR)
            return baseDto1;
//        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
//        if (userRealInfo == null)
//            return BaseDto.error("请先完成真实信息实名认证");
//        if (userRealInfo.getVerified() == 0)
//            return BaseDto.error("请先完成真实信息实名认证");
//        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
//        String phoneNum = userInfoBind.getPhoneNum();
        String phoneNum = userInfoBindService.getPhoneNumByUserId(userId);
        //Jedis //jedis = jedisUtil.getJedis();
        String verifyCode = jedisUtil.get(RedisKeyCreator.capitalPasswordVerifyCodeKey(phoneNum));
        //jedisUtil.close();
        if (verifyCode.equals(code) == false) {
            return BaseDto.error("验证码错误!");
        }
        if (!StringUtils.hasText(phoneNum)) {
            BaseDto baseDto = new BaseDto();
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("该用户还未绑定手机号,请先绑定手机号");
            return baseDto;
        }

        UserPassword userPassword = userPasswordService.getByPrimaryKey(userId);
        if (userPassword == null) {
            userPassword = new UserPassword();
            userPassword.setId(userId);
            userPassword.setCapitalPassword(MD5Util.md5(capitalPassword));
            userPasswordService.insert(userPassword);
            //jedis = jedisUtil.getJedis();
            jedisUtil.del(RedisKeyCreator.capitalPasswordErrorCount(userId));
            //jedisUtil.close();
            return BaseDto.ok("密码设置完成");
        }

        userPassword.setCapitalPassword(MD5Util.md5(capitalPassword));
        userPasswordService.update(userPassword);
        //jedis = jedisUtil.getJedis();
        jedisUtil.del(RedisKeyCreator.capitalPasswordErrorCount(userId));
        //jedisUtil.close();
        return BaseDto.ok("密码设置完成 ");
    }

    @ApiOperation(value = "发送 绑定手机号 的验证码", httpMethod = "GET")
    @ApiImplicitParam(name = "phoneNum", value = "手机号", required = true, dataType = "String", paramType = "query")
    @RequestMapping("/my/safeCentre/bindPhone/verifyCode")
    public BaseDto sendBindPhoneVerifyCode(String phoneNum) {
        return smsUserService.sendSMS(phoneNum, RedisKeyCreator.bindPhoneVerifyCode(phoneNum));
    }

    @ApiOperation(value = "绑定手机号", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "phoneNum", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/bindPhone")
    public BaseDto bindPhone(Integer userId, String phoneNum, String verifyCode) {
        /**
         *能绑定1.手机号没在userInfoBind表中
         *     2.拿phoneNum到userInfoBind表中查出的sphoneNum下没有phoneNum记录
         *不能绑定 1.phoneNum下有该号码
         *        2.sphoneNum下有其他号码;
         **/
        BaseDto baseDto = new BaseDto();
        User user = userService.getByPrimaryKey(userId);
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        String oldNum = "";
        if (user.getType() == UserType.MEMBER || user.getType() == UserType.NONE) {
            oldNum = userInfoBind.getPhoneNum();
        }
        if (user.getType() == UserType.SHAREHOLDER) {
            oldNum = userInfoBind.getSPhoneNum();
        }
        String code = jedisUtil.get(RedisKeyCreator.unbindPhoneVerifyCode(oldNum));
        if (!StringUtils.hasText(code))
            return BaseDto.error("验证码错误");
        Integer type = user.getType();
        if (code.equals(verifyCode) == false) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("验证码错误!");
        } else if (type == UserType.SHAREHOLDER) {
            UserInfoBind bySPhoneNum = userInfoBindService.getBySPhoneNum(phoneNum);
            user.setUsername(phoneNum);
            userService.update(user);
            UserInfoBind sUserInfo = userInfoBindService.getByPrimaryKey(userId);
            String oldSPhoneNum = sUserInfo.getSPhoneNum();
            if (bySPhoneNum != null) {
                return BaseDto.error("已有股东绑定该手机号");
            }
            if (oldSPhoneNum.equals(phoneNum)) {
                return BaseDto.error("请不要绑定相同手机号");
            }
            //更换手机号
            sUserInfo.setSPhoneNum(phoneNum);
            userInfoBindService.update(sUserInfo);
        } else if (type == UserType.MEMBER || type == UserType.NONE) {
            //会员没有绑定手机
            UserInfoBind sInfo = userInfoBindService.getBySPhoneNum(phoneNum);
            user.setUsername(phoneNum);
            userService.update(user);
            //1.手机号没有被使用
            if (sInfo == null) {
                userInfoBind.setPhoneNum(phoneNum);
                userInfoBindService.update(userInfoBind);
                //2.手机已经被股东使用
            } else {
                String mPhoneNum = sInfo.getPhoneNum();
                Integer id = sInfo.getId();
                if (StringUtils.hasText(mPhoneNum)) {
//                    return BaseDto.error("该账号已经绑定了两个手机号");
                    userInfoBind.setPhoneNum(phoneNum);
                    userInfoBindService.update(userInfoBind);
                    return BaseDto.ok("换绑成功");
                } else {
                    /**
                     * 1.原会员userId对应的user表中的deleted改为1
                     * 2.原会员的userBindInfo deleted 改为1
                     * 3.原会员的userRole表 deleted改为1
                     * 4.股东userRole 增加一条记录
                     * 5.股东userBindInfo 增加现在的邮箱
                     * 6.取原会员数据加到股东账户上面
                     *   .1 userCapital
                     *   .2 cashWallet
                     * **/
                    //1.原会员userId对应的user表中的deleted改为1
                    User memberUser = userService.getByPrimaryKey(userId);
                    Integer userType = memberUser.getType();
                    userService.delete(userId);

                    //2.原会员的userBindInfo deleted 改为1
                    String mail = userInfoBind.getMail();
                    userInfoBindService.delete(userId);

                    //3.原会员的userRole表 deleted改为1
                    UserRole memberRole = userRoleService.getByUserIdAndUserRole(userId, UserType.MEMBER);
                    if (memberRole != null) {
                        Integer memberRoleId = memberRole.getId();
                        userRoleService.delete(memberRoleId);
                    }

                    UserRole noneRole = userRoleService.getByUserIdAndUserRole(userId, UserType.NONE);
                    if (noneRole != null) {
                        Integer noneRoleId = noneRole.getId();
                        userRoleService.delete(noneRoleId);
                    }

                    //4.股东userRole 增加一条记录
                    userRoleService.insert(id, userType, UserState.OK);
                    //5.股东userBindInfo 增加现在的邮箱
                    sInfo.setMail(mail);
                    userInfoBindService.update(sInfo);
                    //6.资产转移
                    boolean capitalTransfer = userCapitalService.capitalTransfer(userId, id);
                    boolean cashTransfer = userCapitalService.cashTransfer(userId, id);
                    if (capitalTransfer && cashTransfer) {
                        baseDto.setCode(CodeConst.OK);
                        baseDto.setData("绑定成功");
                    } else {
                        baseDto.setCode(CodeConst.ERROR);
                        return BaseDto.error("资产转移失败");
                    }
                }
            }
        }
//            UserInfoBind totalInfo = userInfoBindService.getByPhoneNum(phoneNum);
//            if (totalInfo != null) {
//                UserInfoBind mInfo = userInfoBindService.getByMPhoneNum(phoneNum);
//                if (mInfo != null) {
//                    return BaseDto.error("已有会员绑定该手机");
//                }
////            有股东身份
//                UserInfoBind sInfo = userInfoBindService.getBySPhoneNum(phoneNum);
//                if (sInfo != null) {
//                    String mPhoneNum = sInfo.getPhoneNum();
//                    Integer id = sInfo.getId();
//                    if (StringUtils.hasText(mPhoneNum)) {
//                        return BaseDto.error("该账号已经绑定了两个手机号");
//                    } else {
//                        /**
//                         * 1.原会员userId对应的user表中的deleted改为1
//                         * 2.原会员的userBindInfo deleted 改为1
//                         * 3.原会员的userRole表 deleted改为1
//                         * 4.股东userRole 增加一条记录
//                         * 5.股东userBindInfo 增加现在的邮箱
//                         * 6.取原会员数据加到股东账户上面
//                         *   .1 userCapital
//                         *   .2 cashWallet
//                         * **/
//                        //1.原会员userId对应的user表中的deleted改为1
//                        User memberUser = userService.getByPrimaryKey(userId);
//                        Integer userType = memberUser.getType();
//                        userService.delete(userId);
//
//                        //2.原会员的userBindInfo deleted 改为1
//                        String mail = mInfo.getMail();
//                        userInfoBindService.delete(userId);
//
//                        //3.原会员的userRole表 deleted改为1
//                        UserRole memberRole = userRoleService.getByUserIdAndUserRole(userId, UserType.MEMBER);
//                        if (memberRole != null) {
//                            Integer memberRoleId = memberRole.getId();
//                            userRoleService.delete(memberRoleId);
//                        }
//                        UserRole noneRole = userRoleService.getByUserIdAndUserRole(userId, UserType.NONE);
//                        if (noneRole != null) {
//                            Integer noneRoleId = noneRole.getId();
//                            userRoleService.delete(noneRoleId);
//                        }
//                        //4.股东userRole 增加一条记录
//                        userRoleService.insert(id, userType, UserState.OK);
//                        //5.股东userBindInfo 增加现在的邮箱
//                        sInfo.setMail(mail);
//                        userInfoBindService.update(sInfo);
//                        //6.资产转移
//                        boolean capitalTransfer = userCapitalService.capitalTransfer(userId, id);
//                        boolean cashTransfer = userCapitalService.cashTransfer(userId, id);
//                        if (capitalTransfer && cashTransfer) {
//                            baseDto.setCode(CodeConst.OK);
//                            baseDto.setData("绑定成功");
//                        } else {
//                            baseDto.setCode(CodeConst.ERROR);
//                            return BaseDto.error("资产转移失败");
//                        }
//                    }
//                }
//            } else {
//                UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
//                if (userInfoBind == null) {
//                    userInfoBind = new UserInfoBind();
//                    userInfoBind.setId(userId);
//                    userInfoBind.setPhoneNum(phoneNum);
//                    userInfoBind.setMail(user.getUsername());
//                    userInfoBindService.insert(userInfoBind);
//                } else {
//                    userInfoBind.setPhoneNum(phoneNum);
//                    userInfoBindService.update(userInfoBind);
//                }
//                baseDto.setCode(CodeConst.OK);
//                baseDto.setData("绑定成功");
//            }
        return baseDto;
    }

    @ApiOperation(value = "发送 解绑手机号 的验证码", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping("/my/safeCentre/unbindPhone/verifyCode")
    public BaseDto sendUnbindPhoneVerifyCode(Integer userId) {
        BaseDto baseDto1 = checkUserId(userId);
        if (baseDto1 != null && baseDto1.getCode() == CodeConst.ERROR)
            return BaseDto.error("该用户不存在");
        String phoneNum = userInfoBindService.getPhoneNumByUserId(userId);
        smsUserService.sendSMS(phoneNum, RedisKeyCreator.unbindPhoneVerifyCode(phoneNum));
        BaseDto baseDto = new BaseDto();
        baseDto.setData("success");
        return baseDto;
    }

    @ApiOperation(value = "解绑手机号", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/unbindPhone")
    public BaseDto unbindPhone(Integer userId, String verifyCode) {
        BaseDto baseDto1 = checkUserId(userId);
        if (baseDto1 != null && baseDto1.getCode() == CodeConst.ERROR)
            return BaseDto.error("该用户不存在");
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        String phoneNum = userInfoBindService.getPhoneNumByUserId(userId);
        BaseDto baseDto = new BaseDto();
        User user = userService.searchByUserid(userId);
        Integer type = user.getType();
        String code = jedisUtil.get(RedisKeyCreator.unbindPhoneVerifyCode(phoneNum));
        if (!StringUtils.hasText(code))
            return BaseDto.error("验证码错误");
        if (code.equals(verifyCode) == false) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("验证码错误!");
        } else {
            if (type == UserType.MEMBER) {
                userInfoBind.setPhoneNum("");
            }
            if (type == UserType.SHAREHOLDER) {
                userInfoBind.setSPhoneNum("");
            }
            userInfoBindService.update(userInfoBind);
            baseDto.setCode(CodeConst.OK);
            baseDto.setData("解绑成功");
        }
        return baseDto;
    }

    /**
     * 发送 绑定邮箱 验证码
     *
     * @param userId
     * @param mail
     * @return
     */
    @RequestMapping("/my/safeCentre/bindMail/verifyCode")
    public BaseDto sendBindMailVerifyCode(Integer userId, String mail) {
        BaseDto baseDto = checkUserId(userId);
        if (baseDto != null && baseDto.getCode() == CodeConst.ERROR)
            return baseDto;
        return mailService.sendMail(mail, RedisKeyCreator.bindMailVerifyCode(userId));
    }

    @ApiOperation(value = "绑定邮箱", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "mail", value = "邮箱", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/bindMail")
    public BaseDto bindMail(int userId, String mail, String verifyCode, String password) {
        BaseDto baseDto = new BaseDto();
        User user = userService.searchByUserid(userId);
        String code = jedisUtil.get(RedisKeyCreator.bindMailVerifyCode(user.getId()));
        if (!StringUtils.hasText(code))
            return BaseDto.error("验证码错误");
        if (code.equals(verifyCode) == false) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("验证码错误!");
            return baseDto;
        }
        if (!frontCommonService.verifyPassword(userId, password))
            return BaseDto.error("密码输入错误");
        else {
            UserInfoBind userInfoBind1 = userInfoBindService.getByMail(mail);
            if (userInfoBind1 != null)
                return BaseDto.error("此邮箱已被绑定");
            UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
            userInfoBind.setMail(mail);
            userInfoBindService.update(userInfoBind);
            baseDto.setCode(CodeConst.OK);
            baseDto.setMsg("绑定成功");
        }
        return baseDto;
    }

    @ApiOperation(value = "发送 解绑邮箱 的验证码", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query")
    @RequestMapping("/my/safeCentre/unbindMail/verifyCode")
    public BaseDto sendUnbindMailVerifyCode(Integer userId) {
        BaseDto baseDto1 = checkUserId(userId);
        if (baseDto1 != null && baseDto1.getCode() == CodeConst.ERROR)
            return BaseDto.error("该用户不存在");
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        if (userInfoBind == null || !StringUtils.hasText(userInfoBind.getMail()))
            return BaseDto.error("该用户并未绑定邮箱");
        String mail = userInfoBind.getMail();
        mailService.sendMail(mail, RedisKeyCreator.unbindMailVerifyCode(mail));
        BaseDto baseDto = new BaseDto();
        baseDto.setData("success");
        return baseDto;
    }

    @ApiOperation(value = "解绑邮箱", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/unbindMail")
    public BaseDto unbindMail(Integer userId, String verifyCode) {
        BaseDto baseDto1 = checkUserId(userId);
        if (baseDto1 != null && baseDto1.getCode() == CodeConst.ERROR)
            return BaseDto.error("该用户不存在");
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        if (userInfoBind == null || !StringUtils.hasText(userInfoBind.getMail()))
            return BaseDto.error("该用户并未绑定邮箱");
        String mail = userInfoBind.getMail();

        BaseDto baseDto = new BaseDto();
        User user = userService.searchByUserid(userId);
        String code = jedisUtil.get(RedisKeyCreator.unbindMailVerifyCode(mail));
        if (!StringUtils.hasText(code))
            return BaseDto.error("验证码错误");
        if (code.equals(verifyCode) == false) {
            baseDto.setCode(CodeConst.ERROR);
            baseDto.setMsg("验证码错误!");
        } else {
            userInfoBind.setMail("");
            userInfoBindService.update(userInfoBind);
            baseDto.setCode(CodeConst.OK);
            baseDto.setData("解绑成功");
        }
        return baseDto;
    }

    @RequestMapping("/my/safeCentre/capitalPassword/changeState")
    public BaseDto chanegCapitalPasswordState(Integer userId, String code) {
        UserPassword userPassword = userPasswordService.getByPrimaryKey(userId);
        if (userPassword == null)
            return BaseDto.error("请先设置资金密码");
        if (!StringUtils.hasText(userPassword.getCapitalPassword()))
            return BaseDto.error("请先设置资金密码");
        Integer required = userPassword.getRequired();
        if (required == 0)
            return openCapitalPassword(userId, code);
        else
            return closeCapitalPassword(userId, code);
    }

    @ApiOperation(value = "设置资金密码为必须", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/capitalPassword/open")
    public BaseDto openCapitalPassword(Integer userId, String code) {
        UserPassword userPassword = userPasswordService.getByPrimaryKey(userId);
        if (userPassword == null)
            return BaseDto.error("请先设置资金密码");
        if (!StringUtils.hasText(userPassword.getCapitalPassword()))
            return BaseDto.error("请先设置资金密码");
        String verifyCode = jedisUtil.get(RedisKeyCreator.verifyCapitalPasswordCode(userId));
        if (!StringUtils.hasText(verifyCode))
            return BaseDto.error("验证码输入错误");
        if (!verifyCode.equals(code))
            return BaseDto.error("验证码输入错误");
        userPasswordService.open(userId);
        BaseDto baseDto = new BaseDto();
        baseDto.setData("success");
        jedisUtil.del(RedisKeyCreator.verifyCapitalPasswordCode(userId));
        return baseDto;

    }

    /**
     * 发送 支付验证设置 验证码
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/my/safeCentre/capitalPassword/verify")
    public BaseDto sendVerifyCapitalPasswordCode(Integer userId) {
        BaseDto baseDto = checkUserId(userId);
        if (baseDto != null && baseDto.getCode() == CodeConst.ERROR)
            return baseDto;

        String phoneNum = userInfoBindService.getPhoneNumByUserId(userId);
        if (!StringUtils.hasText(phoneNum)) {
            return BaseDto.error("请先绑定手机号");
        }

        return smsUserService.sendSMS(phoneNum, RedisKeyCreator.verifyCapitalPasswordCode(userId));

    }

    @ApiOperation(value = "设置资金密码为非必须", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping("/my/safeCentre/capitalPassword/close")
    public BaseDto closeCapitalPassword(Integer userId, String code) {
        UserPassword userPassword = userPasswordService.getByPrimaryKey(userId);
        if (userPassword == null)
            return BaseDto.error("请先设置资金密码");
        if (!StringUtils.hasText(userPassword.getCapitalPassword()))
            return BaseDto.error("请先设置资金密码");
        //Jedis //jedis = jedisUtil.getJedis();
        String verifyCode = jedisUtil.get(RedisKeyCreator.verifyCapitalPasswordCode(userId));
        //jedisUtil.close();
        if (!StringUtils.hasText(verifyCode))
            return BaseDto.error("验证码输入错误");
        if (!verifyCode.equals(code))
            return BaseDto.error("验证码输入错误");
//        if (!frontCommonService.verifyCapitalPassword(userId,capitalPassword))
//            return BaseDto.error("资金密码输入错误");
        userPasswordService.close(userId);
        BaseDto baseDto = new BaseDto();
        baseDto.setData("success");
        jedisUtil.del(RedisKeyCreator.verifyCapitalPasswordCode(userId));
        return baseDto;

    }
}
